//
//  UIView+Nib.m
//  016_CustomUIView
//
//  Created by caoyuan on 6/12/16.
//  Copyright © 2016 caoyuan. All rights reserved.
//

#import "UIView+Nib.h"
@implementation UIView (Nib)

+ (id)initFromNib{
    NSString *nibName = NSStringFromClass([self class]);
    NSArray *objs = [[NSBundle mainBundle] loadNibNamed:nibName
                                                  owner:self
                                                options:nil];
    UIView *view = [objs lastObject];
    return view;
}


@end


@implementation UITableViewCell (Nib)

- (id)initFromNibWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)identifier{
    
    self = [self initWithStyle:style reuseIdentifier:identifier];
    
    if (self) {
        
        NSString *nibName = NSStringFromClass([self class]);
        
        NSArray *objs = [[NSBundle mainBundle] loadNibNamed:nibName
                         
                                                      owner:self
                         
                                                    options:nil];
        
        UIView *view = [objs lastObject];
        
        self.frame = view.frame;
        
        [self addSubview:view];
        
    }
    
    return self;
    
}

@end

@implementation UITableView (Nib)
- (void)registerNibWithClass:(Class)classid
{
    NSString *identifier = NSStringFromClass([classid class]);
    UINib *nib = [UINib nibWithNibName:identifier bundle:[NSBundle mainBundle]];
    [self registerNib:nib forCellReuseIdentifier:identifier];
    
}
@end


@implementation UICollectionView (Nib)

- (void)initFromNib:(Class)cellClass
{
    NSString *identifier = NSStringFromClass([cellClass class]);
    UINib *nib = [UINib nibWithNibName:identifier bundle:[NSBundle mainBundle]];
    
    [self registerNib:nib forCellWithReuseIdentifier:identifier];
}

@end
